package com.sofwin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sofwin.common.ResponseData;
import com.sofwin.mapper.SysResourceMapper;
import com.sofwin.mapper.SysUserMapper;
import com.sofwin.pojo.SysUser;
import com.sofwin.pojo.SysUserExample;
import com.sofwin.pojo.vo.NavMenu;
import com.sofwin.pojo.vo.UserVO;
import com.sofwin.system.UserService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @user: andyliu
 * @date: 2021/9/22 10:31
 * @email 115176513@qq.com
 * @description: 用户所有业务
 */
// 先将服务交给spring，再暴露给注册中心
@Service(interfaceClass = UserService.class,timeout = 10000)
public class UserServiceImpl implements UserService {
    @Autowired
    private SysUserMapper mapper;

    @Autowired
    private SysResourceMapper resourceMapper;

    @Override
    public ResponseData checkLogin(SysUser user) {
        boolean status = false;// 默认登录失败
        SysUserExample example = new SysUserExample();
        SysUserExample.Criteria criteria = example.createCriteria();
        criteria.andLoginNameEqualTo(user.getLoginName());
        List<SysUser> users1 = mapper.selectByExample(example);
        if(users1==null || users1.size()==0){
            return new ResponseData(600,"用户名不存在");
        }
        criteria.andLoginPwdEqualTo(user.getLoginPwd());
        List<SysUser> users = mapper.selectByExample(example);
        if(users==null || users.size()==0){
            return new ResponseData(601,"密码错误");
        }
        return ResponseData.success("登录成功");
    }

    @Override
    public PageInfo<SysUser> queryUser(UserVO user, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        SysUserExample ex = new SysUserExample();
        SysUserExample.Criteria criteria = ex.createCriteria();
        if(user!=null){
            if(!StringUtils.isEmpty(user.getLoginName())){
                criteria.andLoginNameLike("%"+user.getLoginName()+"%");
            }
            if(!StringUtils.isEmpty(user.getMobile())){
                criteria.andMobileEqualTo(user.getMobile());
            }
            if(user.getStatus()!=null && user.getStatus()!=-1){
                criteria.andStatusEqualTo(user.getStatus());
            }
            if(user.getDeptId()!=null && user.getDeptId()!=-1){
                criteria.andDeptIdEqualTo(user.getDeptId());
            }
        }
        List<SysUser> users = mapper.selectByExample(ex);
        return new PageInfo(users);
    }

    @Override
    public boolean saveUser(SysUser user) {
        if(user.getUserId()!=null){
            int flag =mapper.updateByPrimaryKeySelective(user);
            return flag==1?true:false;
        }
        int flag = mapper.insertSelective(user);
        return flag==1?true:false;
    }

    @Override
    public SysUser queryUserById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean deleteUserByIds(Integer[] ids) {
        boolean status = false;
        int num=0;
        for(Integer id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }

    @Override
    public SysUser queryUserByUserName(String loginName) {
        SysUserExample ex = new SysUserExample();
        SysUserExample.Criteria criteria = ex.createCriteria();
        criteria.andLoginNameEqualTo(loginName);
        List<SysUser> users = mapper.selectByExample(ex);
        if(users==null || users.size()==0){
            return null;
        }
        return users.get(0);
    }

    @Override
    public List<NavMenu> queryUserPermissions(Integer roleId) {
        return resourceMapper.queryRoleResources(roleId);
    }
}
